﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using PagedList;

namespace Freshbox2._0.UI.Controllers
{
    public class ManagerShelfLogController : BaseController<log>
    {
        //  理货员上架记录操作

        // GET: /ManagerShelfLog/   上架  

        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }

        public override BLL.BaseBll<log> bll
        {
            get
            {
                return new LogBll();
            }

        }
        ManagerBll managerBll = new ManagerBll();

        StoreBll storeBll = new StoreBll();

        WaresBll waresBll = new WaresBll();

        public class ListLogModel  // logId  logTime  logType  waresCode     status   RealName   userid  storeName    waresName num
        {
          
            public int rate;  //上架频次
            public int avgUpNum; //平均上架数量 
            public int logId;
            public string logTime;
            public string waresCode;
            public string logType;
            public string waresName;
            public string RealName;
            public int userid;
            public string storeName;
            public string logReason;
            public int? status;
            public int num;
            public string downWaresState;

        }

        /// <summary>
        ///  理货员上架记录  分页有问题   速度慢 得要死  查一下  SQL语句  
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="model"></param>                                                                                                                                                                        
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>

        public ActionResult GetNotice(int page, int rows, log model, string date1, string date2)
        {
           int count;
            var listLogModel = new List<ListLogModel>();

            //1. 查询出意见表表里的所有的信息                           
            Expression<Func<log, bool>> condition1 = x => x.logType == 1; //  1： 上架

            if (string.IsNullOrEmpty(date1) || string.IsNullOrEmpty(date2))
            {
                // date1 date2为空，代表第一次查询 或者是没有选日期的查询，默认选查询所有的数据
                condition1 = x => x.logType ==1;  //查询上架的
            }
            else
            {
                // 1.先筛选出这个时间段的 满足要求的信息     下架商品状态 0   在路上  1   .已送达仓库
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition1 = x => x.logTime >= datetime1 && x.logTime <= datetime2 && x.logType == 1; //  1： 上架
            }
            //model, x => true, true, x => x.logId, rows, page, out count
            var listNotice = bll.Search().Where(condition1).Select(x => new
            {
                x.logId,
                x.managerCode,
                x.status,
                x.logTime,
                logType = x.logType == 1 ? "上架" : "下架",
                x.storeCode,
                x.waresCode,
                x.logReason,
                waresName = x.wares.waresName,
                x.beforeUpdate,
                x.afterUpdate,

            }).ToList() ;

            foreach (var oneCode in listNotice)
            {
                int managerid = Convert.ToInt32(oneCode.managerCode);

                //根据订单详情表里的商品编号  进行查询用户表的相关信息 

                var list2 = managerBll.Search(x => x.userid == managerid).Select(x => new { x.RealName, x.userid, }).ToList();

                var storeList = storeBll.Search(x => x.storeid == oneCode.storeCode).Select(x => new { x.storeid, x.storeName, x.storeDress, x.property }).ToList();

                //判断 驳回的操作是否被理货员处理  >=1 代表已经处理
                int isRun = bll.Search(x => x.managerCode == oneCode.managerCode && x.logType == 1 && x.waresCode == oneCode.waresCode && x.storeCode == oneCode.storeCode && x.logTime >= oneCode.logTime).Count();

                //&& wareList.Count() != 0
                if (list2.Count != 0 && storeList.Count != 0)
                {
                    ListLogModel logmodel = new ListLogModel();
                    logmodel.logId = oneCode.logId;
                    logmodel.logType = oneCode.logType;   //double aaaaa = Math.abs(你的变量);  求绝对值
                    logmodel.num = Math.Abs(Convert.ToInt32(oneCode.beforeUpdate) - Convert.ToInt32(oneCode.afterUpdate));
                    if (isRun >= 1)
                    {
                        logmodel.status = 2;//采销人员 已处理     
                    }
                    else
                    {
                        logmodel.status = oneCode.status;

                    }
                    logmodel.waresCode = oneCode.waresCode;
                    logmodel.waresName = oneCode.waresName;
                    logmodel.storeName = storeList.FirstOrDefault().storeName;
                    logmodel.userid = list2.FirstOrDefault().userid;
                    logmodel.logReason = oneCode.logReason;
                    logmodel.RealName = list2.FirstOrDefault().RealName;
                    logmodel.logTime = Convert.ToDateTime(oneCode.logTime).ToString("yyyy-MM-dd HH:mm:ss");
                    listLogModel.Add(logmodel);
                }
            }
            // 为了更改读取的状态     1.读取  0.未读取
            var listNotice2 = bll.Search(x => x.IsRead == 0).Where(condition1).Select(x => new
            {
                x.logId,
                x.managerCode,
                x.status,
                x.logTime,
                logType = x.logType,
                x.storeCode,
                x.waresCode,
                x.logReason,
                x.afterUpdate,
                x.beforeUpdate, 

            }).ToList();
            foreach (var item in listNotice2)
            {
                log model1 = new log();
                model1.logId = item.logId;
                model1.managerCode = item.managerCode;
                int logtype = Convert.ToInt32(item.logType);
                model1.logType = 1;
                model1.logTime = item.logTime;

                model1.waresCode = item.waresCode;
                model1.logReason = item.logReason;

                model1.afterUpdate = item.afterUpdate;
                model1.beforeUpdate = item.beforeUpdate;
                model1.status = item.status;
                model1.storeCode = item.storeCode;
                model1.IsRead = 1;//已读
                if (bll.Modify(model1) > 0)
                {

                }
            }
           var listLog = listLogModel.OrderByDescending(x=>x.logId).ToPagedList(page, rows).ToList();
           return Json(new { rows = listLog, total = listLogModel.Count() });//listLog  listLogModel
        }


        public ActionResult GetNoticeEf(int page, int rows, string waresName, string sort, string order)
        {
            var listLogModel = new List<ListLogModel>();

            var listOrderLogModel = new List<ListLogModel>();
            //1. 查询出意见表表里的所有的信息                             
            Expression<Func<log, bool>> condition1 = x => x.logType == 1; //  1： 上架
            if (String.IsNullOrEmpty(waresName))
            {
                var listNotice = bll.Search(condition1).GroupBy(x => x.waresCode).Select(x => new
                {
                    x.FirstOrDefault().waresCode,
                    waresName = x.FirstOrDefault().wares.waresName,//这个就是主从表关联
                    before = x.Sum(y => y.beforeUpdate),
                    after = x.Sum(y => y.afterUpdate),
                    rate = x.Count(),
                }).ToList();

                foreach (var oneCode in listNotice)
                {
                    ListLogModel logmodel = new ListLogModel();
                    int num = Math.Abs(Convert.ToInt32(oneCode.before) - Convert.ToInt32(oneCode.after));
                    logmodel.waresCode = oneCode.waresCode;
                    logmodel.waresName = oneCode.waresName;
                    logmodel.rate = oneCode.rate;
                    logmodel.avgUpNum = num / oneCode.rate;
                    listLogModel.Add(logmodel);
                }

            }
            else  //根据 商品 名称模糊查询
            {
                condition1 = x => x.logType == 4 && x.wares.waresName.Contains(waresName); //上架成功的

                var listNotice = bll.Search().Where(condition1).GroupBy(x => x.waresCode).Select(x => new
                {
                    x.FirstOrDefault().waresCode,
                    waresName = x.FirstOrDefault().wares.waresName,//这个就是主从表关联
                    before = x.Sum(y => y.beforeUpdate),
                    after = x.Sum(y => y.afterUpdate),
                    rate = x.Count(),

                }).ToList();
                foreach (var oneCode in listNotice)
                {
                    ListLogModel logmodel = new ListLogModel();
                    int num = Math.Abs(Convert.ToInt32(oneCode.before) - Convert.ToInt32(oneCode.after));
                    logmodel.waresCode = oneCode.waresCode;
                    logmodel.waresName = oneCode.waresName;
                    logmodel.rate = oneCode.rate;
                    logmodel.avgUpNum = num / oneCode.rate;
                    listLogModel.Add(logmodel);

                }
            }


            if (sort == "avgUpNum" && order == "desc")
            {
                listOrderLogModel = listLogModel.OrderByDescending(x => x.avgUpNum).ToPagedList(page, rows).ToList(); //pageNum 页码数    pagesize  每页显示几条数据
            }
            if (sort == "avgUpNum" && order == "asc")
            {
                listOrderLogModel = listLogModel.OrderBy(x => x.avgUpNum).ToPagedList(page, rows).ToList(); //pageNum 页码数    pagesize  每页显示几条数据
            }
            if (sort == "rate" && order == "desc")
            {
                listOrderLogModel = listLogModel.OrderByDescending(x => x.rate).ToPagedList(page, rows).ToList();
            }
            if (sort == "rate" && order == "asc")
            {
                listOrderLogModel = listLogModel.OrderBy(x => x.rate).ToPagedList(page, rows).ToList();
            }

            return Json(new { rows = listOrderLogModel, total = listLogModel.Count() });
  
        }


        public ActionResult GetNoticeEf1(int page, int rows, log model, string date1, string date2)
        {
            int count;
            var listLogModel = new List<ListLogModel>();

            //1. 查询出意见表表里的所有的信息                           
            Expression<Func<log, bool>> condition1 = x => x.logType == 1; //  1： 上架

            if (string.IsNullOrEmpty(date1) || string.IsNullOrEmpty(date2))
            {
                // date1 date2为空，代表第一次查询 或者是没有选日期的查询，默认选查询所有的数据
                condition1 = x => x.logType == 1;  //查询上架的
            }
            else
            {
                // 1.先筛选出这个时间段的 满足要求的信息      
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition1 = x => x.logTime >= datetime1 && x.logTime <= datetime2 && x.logType == 1; //  1： 上架
            }
            //model, x => true, true, x => x.logId, rows, page, out count   (condition, isAsc, order, pageSize, pageIndex, out count);

            var listNotice = bll.Search(condition1, false, x => x.logId, rows, page, out count).Select(x => new
            {
                x.logId,
                x.managerCode,
                x.status,
                x.logTime,
                logType = x.logType == 1 ? "上架" : "下架",
                x.storeCode,
                x.waresCode,
                x.logReason,
                waresName = x.wares.waresName,//这个就是主从表关联
                x.beforeUpdate,
                x.afterUpdate,

            }).ToList();

            foreach (var oneCode in listNotice)
            {
                int managerid = Convert.ToInt32(oneCode.managerCode);

                //根据订单详情表里的商品编号  进行查询用户表的相关信息 

                var list2 = managerBll.Search(x => x.userid == managerid).Select(x => new { x.RealName, x.userid, }).ToList();

                var storeList = storeBll.Search(x => x.storeid == oneCode.storeCode).Select(x => new { x.storeid, x.storeName, x.storeDress, x.property }).ToList();

                //判断 驳回的操作是否被理货员处理  >=1 代表已经处理
                int isRun = bll.Search(x => x.managerCode == oneCode.managerCode && x.logType == 1 && x.waresCode == oneCode.waresCode && x.storeCode == oneCode.storeCode && x.logTime >= oneCode.logTime).Count();

                //&& wareList.Count() != 0
                if (list2.Count != 0 && storeList.Count != 0)
                {
                    ListLogModel logmodel = new ListLogModel();
                    logmodel.logId = oneCode.logId;
                    logmodel.logType = oneCode.logType;   //double aaaaa = Math.abs(你的变量);  求绝对值
                    logmodel.num = Math.Abs(Convert.ToInt32(oneCode.beforeUpdate) - Convert.ToInt32(oneCode.afterUpdate));
                    if (isRun >= 1)
                    {
                        logmodel.status = 2;//采销人员 已处理     
                    }
                    else
                    {
                        logmodel.status = oneCode.status;

                    }
                    logmodel.waresCode = oneCode.waresCode;
                    logmodel.waresName = oneCode.waresName;
                    logmodel.storeName = storeList.FirstOrDefault().storeName;
                    logmodel.userid = list2.FirstOrDefault().userid;
                    logmodel.logReason = oneCode.logReason;
                    logmodel.RealName = list2.FirstOrDefault().RealName;
                    logmodel.logTime = Convert.ToDateTime(oneCode.logTime).ToString("yyyy-MM-dd HH:mm:ss");
                    listLogModel.Add(logmodel);
                }
            }
            // 为了更改读取的状态     1.读取  0.未读取
            var listNotice2 = bll.Search(x => x.IsRead == 0).Where(condition1).Select(x => new
            {
                x.logId,
                x.managerCode,
                x.status,
                x.logTime,
                logType = x.logType,
                x.storeCode,
                x.waresCode,
                x.logReason,
                x.afterUpdate,
                x.beforeUpdate,

            }).ToList();
            foreach (var item in listNotice2)
            {
                log model1 = new log();
                model1.logId = item.logId;
                model1.managerCode = item.managerCode;
                int logtype = Convert.ToInt32(item.logType);
                model1.logType = 1;
                model1.logTime = item.logTime;

                model1.waresCode = item.waresCode;
                model1.logReason = item.logReason;

                model1.afterUpdate = item.afterUpdate;
                model1.beforeUpdate = item.beforeUpdate;
                model1.status = item.status;
                model1.storeCode = item.storeCode;
                model1.IsRead = 1;//已读
                if (bll.Modify(model1) > 0)
                {

                }
            }
            return Json(new { rows = listLogModel, total = count });//listLog  listLogModel
            //var listLog = listLogModel.OrderByDescending(x => x.logId).ToPagedList(page, rows).ToList();
            //return Json(new { rows = listLog, total = listLogModel.Count() });//listLog  listLogModel
        }



    }
}
